package cn.sxt.yxl.user.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.user.dao.IAAdminsDao;
import cn.sxt.yxl.user.dao.IARoleDao;
import cn.sxt.yxl.user.pojo.AAdmins;
import cn.sxt.yxl.user.pojo.ARole;
import cn.sxt.yxl.user.service.IUserService;

@Service("userService")
public class UserServiceImpl implements IUserService{
	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	
	
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap) {		
		return this.adminsDao.findOne(condMap);				
	}


	@Override
	public JSONObject saveOneAdminService(AAdmins admin) {
		JSONObject resultJSON = new JSONObject();
		int res=this.adminsDao.saveOne(admin);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", admin.getId());
			dataJson.put("effectCount", res);
			
			resultJSON.put("data",dataJson);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}


	@Override
	public JSONObject updateOneAdminService(AAdmins admin) {
		JSONObject resultJSON = new JSONObject();
		int res=this.adminsDao.updateOne(admin);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", admin.getId());
			dataJson.put("effectCount", res);
			
			resultJSON.put("data",dataJson);
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}


	@Override
	public JSONObject deleteOneAdminService(AAdmins admin) {
		JSONObject resultJSON = new JSONObject();
		int res=this.adminsDao.deleteOne(admin);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", admin.getId());
			dataJson.put("effectCount", res);
			
			resultJSON.put("data",dataJson);
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}


	@Override
	public List<AAdmins> findCondListAdminService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil !=null){
			//获取分页信息
			Page page=PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<AAdmins> adminsList=this.adminsDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return adminsList;
		}
		return this.adminsDao.findCondList(condMap);
	}
//------------------------------------------------------
//------------------------------------------------------
			
	@Override
	public ARole findOneRoleService(Map<String, Object> condMap) {		
		return this.roleDao.findOne(condMap);				
	}


	@Override
	public JSONObject saveOneRoleService(ARole admin) {
		JSONObject resultJSON = new JSONObject();
		int res=this.roleDao.saveOne(admin);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", admin.getId());
			dataJson.put("effectCount", res);
			
			resultJSON.put("data",dataJson);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}


	@Override
	public JSONObject updateOneRoleService(ARole admin) {
		JSONObject resultJSON = new JSONObject();
		int res=this.roleDao.updateOne(admin);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", admin.getId());
			dataJson.put("effectCount", res);
			
			resultJSON.put("data",dataJson);
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}


	@Override
	public JSONObject deleteOneRoleService(ARole admin) {
		JSONObject resultJSON = new JSONObject();
		int res=this.roleDao.deleteOne(admin);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", admin.getId());
			dataJson.put("effectCount", res);
			
			resultJSON.put("data",dataJson);
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}


	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil !=null){
			//获取分页信息
			Page page=PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<ARole> roleList=this.roleDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return roleList;
		}
		return this.roleDao.findCondList(condMap);
	}
}
